package com.nacrtstudio.webgiftshop.model.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.nacrtstudio.webgiftshop.core.dao.service.CreateCommand;
import com.nacrtstudio.webgiftshop.core.dao.service.DeleteCommand;
import com.nacrtstudio.webgiftshop.core.dao.service.ReadCommand;
import com.nacrtstudio.webgiftshop.core.dao.service.UpdateCommand;
import com.nacrtstudio.webgiftshop.model.service.Manager;

public class ManagerImpl<T> implements Manager<T>{
	private T object;
	private CreateCommand<T> createCommand;
	private ReadCommand<T> readCommand;
	private UpdateCommand<T> updateCommand;
	private DeleteCommand<T> deleteCommand;
	
	public T getObject() {
		return object;
	}

	public void setObject(T object) {
		this.object = object;
	}

	public CreateCommand<T> getCreateCommand() {
		return createCommand;
	}

	public void setCreateCommand(CreateCommand<T> createCommand) {
		this.createCommand = createCommand;
	}

	public ReadCommand<T> getReadCommand() {
		return readCommand;
	}

	public void setReadCommand(ReadCommand<T> readCommand) {
		this.readCommand = readCommand;
	}

	public UpdateCommand<T> getUpdateCommand() {
		return updateCommand;
	}

	public void setUpdateCommand(UpdateCommand<T> updateCommand) {
		this.updateCommand = updateCommand;
	}

	public DeleteCommand<T> getDeleteCommand() {
		return deleteCommand;
	}

	public void setDeleteCommand(DeleteCommand<T> deleteCommand) {
		this.deleteCommand = deleteCommand;
	}

	@Transactional
	public void create() {
		getCreateCommand().setObject(object);
		getCreateCommand().execute();
	}
	
	@Transactional
	public void update() {
		getUpdateCommand().setObject(object);
		getUpdateCommand().execute();
	}
	@Transactional
	public void delete(){
		getDeleteCommand().setObject(object);
		getDeleteCommand().execute();
	}
	
	@Transactional
	public List<T> getAll(Class<T> clazz){
		getReadCommand().setType(clazz);
		return getReadCommand().execute();
	}
	
	public List<T> getAllSortedByProperty(Class<T> clazz, String property){
		return getReadCommand().getAllAscSortByProperty(clazz, property);
	}
	
	public List<T> get(Class<T> clazz, T object){
		return getReadCommand().execute(clazz, object);
	}
	
	public T getByPropertyValue(Class<T> searchForType, String property, String value){
		return (T) getReadCommand().execute(searchForType, property, value);
	}

	public void update(T object) {
		getUpdateCommand().setObject(object);
		getUpdateCommand().execute();
	}

	public List<T> getAllByMaxValue(Class<T> class1, String property) {
		return getReadCommand().execute(class1, property);
	}

	public T getByPropertyID(Class<T> searchForType, String string,
			Long productId) {
		return (T) getReadCommand().execute(searchForType, string, productId);
	}
}
